package com.qf.cofnig;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    // 处理业务的交换机
    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange("order-exchange", true, false);
    }

    // 处理订单业务的队列
    @Bean
    public Queue orderQueue() {
        // 给业务队列配置了死信交换机和死信路由键
        // 只要业务队列中存在死信后就会自动的转发到过去
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-routing-key", "dead.error"); // 设置死信的路由键
        map.put("x-dead-letter-exchange", "dead-exchange"); // 设置死信的交换机
        return new Queue("order-queue", true, false, false, map);
    }

    @Bean
    public Binding orderQueueToOrderExchange() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with("order.*");
    }

    // 死信交换机
    @Bean
    public TopicExchange deadExchange() {
        return new TopicExchange("dead-exchange", true, false);
    }

    @Bean
    public Queue deadQueue() {
        return new Queue("dead-queue", true, false, false);
    }

    @Bean
    public Binding deadQueueToDeadExchange() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("dead.*");
    }

    @Bean
    public Queue ttl10Queue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 10000); // 超时时间是10s

        // 消息超时后要转到死信队列中
        map.put("x-dead-letter-routing-key", "dead.error"); // 设置死信的路由键
        map.put("x-dead-letter-exchange", "dead-exchange"); // 设置死信的交换机

        return new Queue("ttl-10-queue", true, false, false, map);
    }

    @Bean
    public Queue ttl5Queue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 5000); // 超时时间是10s

        // 消息超时后要转到死信队列中
        map.put("x-dead-letter-routing-key", "dead.error"); // 设置死信的路由键
        map.put("x-dead-letter-exchange", "dead-exchange"); // 设置死信的交换机

        return new Queue("ttl-5-queue", true, false, false, map);
    }


    @Bean
    public Queue maxQueue() {
        Map<String, Object> map = new HashMap<>();

        map.put("x-max-length", 10); // 设置队列的长度是10，最多能放10个消息
        // 消息超时后要转到死信队列中
        map.put("x-dead-letter-routing-key", "dead.error"); // 设置死信的路由键
        map.put("x-dead-letter-exchange", "dead-exchange"); // 设置死信的交换机

        return new Queue("max-queue", true, false, false, map);
    }

    @Bean
    public Binding maxQueueBindOrderExchange() {
        return BindingBuilder.bind(maxQueue()).to(orderExchange()).with("max.*");
    }


    @Bean
    public Binding ttl10QueueBindOrderExchange() {
        return BindingBuilder.bind(ttl10Queue()).to(orderExchange()).with("ttl.10");
    }

    @Bean
    public Binding ttl5QueueBindOrderExchange() {
        return BindingBuilder.bind(ttl5Queue()).to(orderExchange()).with("ttl.5");
    }


}
